package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.ShopDao;
import com.aaa.vegetablemarketexpress.dao.ShopProductDao;
import com.aaa.vegetablemarketexpress.dao.UserDao;
import com.aaa.vegetablemarketexpress.entity.Shop;
import com.aaa.vegetablemarketexpress.entity.ShopProduct;
import com.aaa.vegetablemarketexpress.service.ShopProductService;
import com.aaa.vegetablemarketexpress.service.ShopService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ShopProductServiceImpl implements ShopProductService {

    @Resource
    private ShopProductDao shopProductDao;

    @Resource
    private ShopDao shopDao;

    @Resource
    private UserDao userDao;
    @Resource
    private ShopService shopService; // 注入店铺服务

    /**
     *
     * @param id
     * @return
     */
    @Override
    public ShopProduct selectShopProductById(Long id) {
        return shopProductDao.selectShopProductById(id);
    }

    @Override
    public PageInfo<ShopProduct> selectShopProductList(Page<ShopProduct> page) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<ShopProduct> list = shopProductDao.selectShopProductList(page.getData());
        return new PageInfo<>(list);
    }

    @Override
    public List<Long> selectProductIdsByShopId(Long shopId) {
        return shopProductDao.selectProductIdsByShopId(shopId);
    }

    @Override
    public List<Long> selectShopIdsByProductId(Long productId) {
        return shopProductDao.selectShopIdsByProductId(productId);
    }

    @Override
    public int insertShopProduct(ShopProduct shopProduct) {
        return shopProductDao.insertShopProduct(shopProduct);
    }

    @Override
    public int batchInsertShopProduct(List<ShopProduct> shopProductList) {
        return shopProductDao.batchInsertShopProduct(shopProductList);
    }

    @Override
    public int deleteShopProductById(Long id) {
        return shopProductDao.deleteShopProductById(id);
    }

    @Override
    public int deleteShopProduct(Long shopId, Long productId) {
        return shopProductDao.deleteShopProduct(shopId, productId);
    }

    @Override
    public int deleteShopProductByShopId(Long shopId) {
        return shopProductDao.deleteShopProductByShopId(shopId);
    }

    @Override
    public int deleteShopProductByProductId(Long productId) {
        return shopProductDao.deleteShopProductByProductId(productId);
    }

    @Override
    public int checkShopProductExist(Long shopId, Long productId) {
        return shopProductDao.checkShopProductExist(shopId, productId);
    }

    @Transactional
    @Override
    public int updateShopProducts(Long shopId, List<Long> productIds) {
        // 先删除该店铺所有商品关联
        int deleteCount = deleteShopProductByShopId(shopId);

        // 批量添加新的商品关联
        if (productIds != null && !productIds.isEmpty()) {
            Date now = new Date();
            String createBy = "system"; // 实际应从登录用户获取

            for (Long productId : productIds) {
                ShopProduct shopProduct = new ShopProduct();
                shopProduct.setShopId(shopId);
                shopProduct.setProductId(productId);
                shopProduct.setCreateTime(now);
                shopProduct.setCreateBy(createBy);
                insertShopProduct(shopProduct);
            }

            return productIds.size();
        }

        return 0;
    }

    /**
     * 为店铺添加商品（包含权限校验）
     * 确保只有店铺所属商家才能添加商品
     */
    @Transactional
    @Override
    public int addProductToShop(Long shopId, Long productId, Long currentUserId) {
        //  验证店铺是否存在
        Shop shop = shopDao.selectShopById(shopId);
        if (shop == null) {
            throw new RuntimeException("店铺不存在");
        }

        // 2. 验证当前用户是否为该店铺的商家（权限校验）
        // 检查用户是否为商家角色
        int isMerchant = userDao.checkUserIsMerchant(currentUserId);
        if (isMerchant <= 0) {
            throw new RuntimeException("没有权限添加商品，您不是商家");
        }

        // 3. 验证该商品是否已关联到店铺
        int existCount = checkShopProductExist(shopId, productId);
        if (existCount > 0) {
            throw new RuntimeException("该商品已添加到店铺");
        }

        // 4. 添加商品关联
        ShopProduct shopProduct = new ShopProduct();
        shopProduct.setShopId(shopId);
        shopProduct.setProductId(productId);
        shopProduct.setCreateTime(new Date());
        shopProduct.setCreateBy(userDao.selectNickNameByUserId(currentUserId));

        return insertShopProduct(shopProduct);
    }

    @Override
    public ShopProduct selectShopProduct(Long shopId, Long productId) {
        return shopProductDao.selectShopProduct(shopId, productId);
    }
    @Override
    public String getShopNameByProductId(Long productId) {
        // 1. 通过productId查询关联的shopId
        List<Long> shopIds = shopProductDao.selectShopIdsByProductId(productId);
        if (shopIds == null || shopIds.isEmpty()) {
            return "无关联店铺";
        }
        Long shopId = shopIds.get(0); // 假设一个产品只关联一个店铺

        // 2. 通过shopId查询店铺名称
        Shop shop = shopService.selectShopById(shopId);
        return shop != null ? shop.getShopName() : "店铺不存在";
    }
}
